package com.mlnx.service.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/4/22.
 */
public class DateUtils {

    //格式化日期
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public enum TimeState {
        MORNING, AFTERNOON, NIGHT
    }

    public static long calTimeInterval(String startTime, String stopTime, String dateFormat)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.parse(stopTime).getTime() - format.parse(startTime).getTime();
    }

    public static String format(long time, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(new Date(time));
    }

    public static Date formatDate(Date date){
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Date parse(String time, String dateFormat) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.parse(time);
    }

    /**
     * 判断是否为同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar
                .MONTH) == calendar2.get(Calendar.MONTH) && calendar1.get(Calendar.DAY_OF_MONTH) ==
                calendar2.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 返回时长 00：00：00
     *
     * @param duration
     * @return
     */
    public static String getDuration(long duration) {
        duration /= 1000;
        int second = (int) (duration % 60);
        duration /= 60;
        int minute = (int) (duration % 60);
        duration /= 60;
        int hour = (int) (duration % 60);

        StringBuilder builder = new StringBuilder();
        builder.append(String.format("%02d", hour));
        builder.append(":");
        builder.append(String.format("%02d", minute));
        builder.append(":");
        builder.append(String.format("%02d", second));
        return builder.toString();
    }

    /**
     * 返回时间状态
     * @param time
     * @return
     */
    public static TimeState getTimeState(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);

        if (calendar.get(Calendar.HOUR_OF_DAY) > 5 && calendar.get(Calendar.HOUR_OF_DAY) < 12) {
            return TimeState.MORNING;
        } else if (calendar.get(Calendar.HOUR_OF_DAY) < 18) {
            return TimeState.AFTERNOON;
        } else {
            return TimeState.NIGHT;
        }
    }

    /**
     * 根据用户生日计算年龄
     */
    public static int getAgeByBirthday(long birthday) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(new Date(birthday))) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(new Date(birthday));
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        }
        return age;
    }

    public static Date getFirstDayOfMonth(int year, int month) throws ParseException {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);

        String firstDayOfMonth = sdf.format(cal.getTime());
        return sdf.parse(firstDayOfMonth);
    }

    /**
     * 获取未来第几天的日期(- 操作) 或者 未来 第几天的日期( + 操作)
     *
     * @param future
     * @return
     */
    public static Date getFutureDate(int future) throws ParseException {
        return getFutureDate(new Date(),future);
    }

    public static Date getFutureDate(Date current,int future) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(current);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + future);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return format.parse(result);
    }

    /**
     * 获取过去第几天的日期(- 操作) 或者 未来 第几天的日期( + 操作)
     *
     * @param past
     * @return
     */
    public static Date getPastDate(int past) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return format.parse(result);
    }

}
